package com.xhwl.centiotdata.suppliersystem.keytopLocal;


import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.alibaba.fastjson.JSON;
import com.xhwl.centiotdata.pojo.constant.KeyTopLocalConstant;
import com.xhwl.centiotdata.pojo.dto.device.DeviceIntegrationConfigDTO;
import com.xhwl.centiotdata.pojo.dto.keytoplocal.DeviceKeyTopLocalDTO;
import com.xhwl.common.dto.cent.device.DeviceDTO;
import com.xhwl.common.enums.device.DeviceOnlineStatusEnum;
import com.xhwl.common.pojo.cent.device.Device;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author msy
 */
@Slf4j
@Service
public class DeviceKeyTopLocalHandler {

    @Autowired
    private GeneralKeyTopLocalHandler generalKeyTopLocalHandler;

    public List<DeviceKeyTopLocalDTO> pullDevice(DeviceIntegrationConfigDTO configDTO){
        Integer projectId = configDTO.getProjectId();
        Integer productId = configDTO.getProductId();
        String ipAndPort = configDTO.getIpAndPort();
        String loginName = configDTO.getLoginName();
        String password = configDTO.getPassword();

        JSONObject result = generalKeyTopLocalHandler.post(KeyTopLocalConstant.DOOR_LIST, null, projectId,
                productId, ipAndPort, loginName, password);

        List<DeviceKeyTopLocalDTO> deviceKeyTopLocalDTOList = handleData(result);
        log.info("deviceKeyTopLocalDTOList : {}", deviceKeyTopLocalDTOList);
        return deviceKeyTopLocalDTOList;
    }


    private List<DeviceKeyTopLocalDTO> handleData(JSONObject result) {
        List<DeviceKeyTopLocalDTO> deviceKeyTopLocalDTOList = new ArrayList<>();

        JSONArray data = result.getJSONArray("data");
        data.forEach(item -> {
            DeviceKeyTopLocalDTO dto = JSON.parseObject(JSON.toJSONString(item), DeviceKeyTopLocalDTO.class);
            deviceKeyTopLocalDTOList.add(dto);
        });
        return deviceKeyTopLocalDTOList;
    }


    public List<DeviceDTO> handleDevice(DeviceIntegrationConfigDTO configDTO, List<DeviceKeyTopLocalDTO> deviceKeyTopLocalDTOList) {
        return deviceKeyTopLocalDTOList.stream().map(deviceKeyTopDTO -> {
            DeviceDTO deviceDTO = new DeviceDTO();
            deviceDTO.setProjectId(configDTO.getProjectId());
            deviceDTO.setProductId(configDTO.getProductId());
            deviceDTO.setThirdDeviceId(deviceKeyTopDTO.getId());
            deviceDTO.setCategoryId(configDTO.getCategoryEnum().getId());
            deviceDTO.setDeviceTypeId(configDTO.getDeviceType());
            deviceDTO.setName(deviceKeyTopDTO.getName());
            // 99代表设备离线
            deviceDTO.setOnlineStatus(Integer.valueOf(99).equals(deviceKeyTopDTO.getStatus()) ? DeviceOnlineStatusEnum.OFFLINE.id : DeviceOnlineStatusEnum.ONLINE.id);
            return deviceDTO;
        }).collect(Collectors.toList());
    }



    public List<Device> handlePedestrianDeviceOnlineStatus(DeviceIntegrationConfigDTO configDTO, List<DeviceKeyTopLocalDTO> deviceKeyTopLocalDTOList) {

        List<Device> deviceList = new ArrayList<>();
        for(DeviceKeyTopLocalDTO item : deviceKeyTopLocalDTOList) {
            Device device = new Device();
            device.setProjectId(configDTO.getProjectId());
            device.setThirdDeviceId(item.getId());
            device.setDeviceTypeId(configDTO.getDeviceType());
            device.setOnlineStatus(Integer.valueOf(99).equals(item.getStatus()) ? DeviceOnlineStatusEnum.OFFLINE : DeviceOnlineStatusEnum.ONLINE);
            deviceList.add(device);
        }
        return deviceList;
    }
}
